package com.zbkj.admin.controller.brand;

import com.zbkj.common.annotation.LogControllerAnnotation;
import com.zbkj.common.enums.MethodType;
import com.zbkj.common.model.agent.BrandAgentItems;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.AgentUserRegionResponse;
import com.zbkj.common.response.BrandAgentCategoryListResponse;
import com.zbkj.common.response.BrandAgentDetailResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.utils.SecurityUtil;
import com.zbkj.common.vo.LoginUserVo;
import com.zbkj.service.service.*;
import com.zbkj.service.util.BrandUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.zbkj.common.response.BrandAgentPageDataResponse;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌商代理设置 前端控制器
 */
@Slf4j
@RestController
@RequestMapping("api/admin/brand/agent")
@Api(tags = "品牌商代理设置") //配合swagger使用

public class BrandAgentController {

    @Autowired
    private BrandAgentService brandAgentService;

    @Autowired
    private AgentConfigService agentConfigService;

    @Autowired
    private UserAgentService userAgentService;

    @Autowired
    private BrandAgentItemsService brandAgentItemsService;

    /**
     * 分类列表
     * @return
     */
    @ApiOperation(value = "分类列表", notes = "获取总平台设给该品牌商设置的分类")
    @RequestMapping(value = "/category/list", method = RequestMethod.GET)
    public CommonResult<List<BrandAgentCategoryListResponse>> getPlatformCategoryList() {
        List<BrandAgentCategoryListResponse> brandAgentCategoryListResponses=  agentConfigService.getPlatformCategoryList();
        List<BrandAgentItems> agentItems = getAgentItems();
        if (agentItems!=null&&agentItems.size()>0){
            brandAgentCategoryListResponses.forEach(e -> {
                agentItems.stream().filter(agentItem -> agentItem.getCategoryId().equals(e.getCategoryId())).forEach(e1 -> {
                    e.setSuperiorBrokerage(e1.getBrokerage());
                });
            });
        }

        return CommonResult.success(brandAgentCategoryListResponses);
    }
    public List<BrandAgentItems> getAgentItems() {
        Integer brandId = BrandUtil.getBrandIdByUrl();
        List<BrandAgentItems>  brandAgentItems = brandAgentItemsService.getAgentItems(brandId);
        return brandAgentItems;
    }

    /**
     * 分页显示品牌商代理设置表
     *
     * @param request          搜索条件
     * @param pageParamRequest 分页参数
     * @author dazongzi
     * @since 2025-07-01
     */
    //@PreAuthorize("hasAuthority('platform:activitystyle:list')")
    @ApiOperation(value = "分页列表") //配合swagger使用
    @RequestMapping(value = "/page/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<BrandAgentPageDataResponse>> getPageList(@Validated BrandAgentSearchRequest request,
                                                                        @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<BrandAgentPageDataResponse> brandAgentConfigCommonPage = CommonPage.restPage(brandAgentService.getPageList(request, pageParamRequest));
        return CommonResult.success(brandAgentConfigCommonPage);
    }



    /**
     * 新增品牌商代理设置表
     * @param brandAgentSaveRequest 新增参数
     * @author dazongzi
     * @since 2025-07-01
     */
    @ApiOperation(value = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public CommonResult<String> save(@RequestBody @Validated BrandAgentSaveRequest brandAgentSaveRequest) {
        if(brandAgentService.create(brandAgentSaveRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    @LogControllerAnnotation(intoDB = true, methodType = MethodType.DELETE, description = "批量删除品牌商代理设置")
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/batch/delete", method = RequestMethod.POST)
    public CommonResult<String> batchDelete(@RequestBody @Validated CommonBatchRequest request) {
        if (brandAgentService.batchDelete(request.getIdList())) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 修改品牌商代理设置表
     * @param brandAgentConfigRequest 修改参数
     * @author dazongzi
     * @since 2025-07-01
     */
    @ApiOperation(value = "修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CommonResult<String> update(@RequestBody @Validated BrandAgentSaveRequest brandAgentConfigRequest) {
        if (brandAgentService.updateBrand(brandAgentConfigRequest)) {
            return CommonResult.success();
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 查询品牌商代理设置表信息
     * @param id Integer
     * @author dazongzi
     * @since 2025-07-01
     */
    @ApiOperation(value = "详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<BrandAgentDetailResponse> detail(@PathVariable("id") Integer id) {
        BrandAgentDetailResponse brandAgentDetailResponse = brandAgentService.getDetailById(id);
        List<BrandAgentItems> agentItems = getAgentItems();
        if (agentItems!=null&&agentItems.size()>0){
            if (brandAgentDetailResponse!=null){
                brandAgentDetailResponse.getRegionList().forEach(e1 -> {
                    e1.getItemList().forEach(e -> {
                        agentItems.stream().filter(agentItem -> agentItem.getCategoryId().equals(e.getCategoryId())).forEach(e2 -> {
                            e.setSuperiorBrokerage(e2.getBrokerage());
                        });
                    });
                });
            }
        }


        return CommonResult.success(brandAgentDetailResponse);
    }



    @LogControllerAnnotation(intoDB = true, methodType = MethodType.UPDATE, description = "修改品牌商代理设置启用状态")
//    @PreAuthorize("hasAuthority('platform:admin:role:update:status')")
    @ApiOperation(value = "修改启用状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public CommonResult<Object> updateStatus(@Validated @RequestBody BrandAgentStatusRequest request) {
        if (brandAgentService.updateStatus(request)) {
            return CommonResult.success("修改成功");
        }
        return CommonResult.failed("修改失败");
    }

    /**
     * 当前代理人所属省市区
     * @return
     */
    @ApiOperation(value = "当前代理人所属省市区")
    @RequestMapping(value = "/cur/agent/region", method = RequestMethod.GET)
    public CommonResult<AgentUserRegionResponse> getCurAgentUserRegion() {
        return CommonResult.success(userAgentService.getCurAgentUserRegion());
    }

}



